package com.mentaleye.biz.impl;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mentaleye.bean.CompanyBean;
import com.mentaleye.biz.CompanyBiz;
import com.mentaleye.dal.model.Company;
import com.mentaleye.service.CompanyService;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;
import com.mentaleye.util.ObjectUtil;

@Service
public class CompanyBizImpl implements CompanyBiz {

	private static Logger logger=Logger.getLogger(CompanyBiz.class);
	
	@Autowired private CompanyService dataService;
	
	public CommonResult insertData(CompanyBean dataBean) {
		CommonResult result=new CommonResult();
		try {
			Company data = (Company) ObjectUtil.transfer(dataBean, Company.class);
			if(this.dataService.insertData(data))
			{
				result.setIsSuccess(true);
				result.setResultMsg("插入成功");
			}
			else
			{
				result.setResultMsg("插入失败");
			}
		} catch (Exception e) {
			logger.error("插入企业信息异常", e);
			result.setResultMsg("插入企业信息异常");
		}
		return result;
	}

	public CommonResult updateData(CompanyBean dataBean) {
		CommonResult result=new CommonResult();
		try {
			Company data = (Company) ObjectUtil.transfer(dataBean, Company.class);
			if(this.dataService.updateData(data))
			{
				result.setIsSuccess(true);
				result.setResultMsg("修改成功");
			}
			else
			{
				result.setResultMsg("修改失败");
			}
		} catch (Exception e) {
			logger.error("修改企业信息异常", e);
			result.setResultMsg("修改企业信息异常");
		}
		return result;
	}

	public Map<String, Object> getMapById(int companyId) {
		Map<String, Object> map=null;
		try {
			map= this.dataService.getMapById(companyId);
		} catch (Exception e) {
			logger.error("查询 企业信息异常", e);
		}
		return map;
	}

	public CompanyBean getBeanById(int companyId) {
		CompanyBean companyBean=null;
		try {
			companyBean = (CompanyBean) ObjectUtil.transfer(this.dataService.getRecordById(companyId), Company.class);
		} catch (Exception e) {
			logger.error("查询 企业信息异常", e);
		}
		return companyBean;
	}
	
	@SuppressWarnings("unchecked")
	public List<CompanyBean> getBeanList() {
		List<CompanyBean> beanList=null;
		try {
			List<Company> dataList = this.dataService.getList();
			beanList=ObjectUtil.transferList(dataList, CompanyBean.class);
		} catch (Exception e) {
			logger.error("查询 企业信息异常", e);
		}
		return beanList;
	}

	public void getListByPage(CommonPage page) {
		try {
			page.setPageData(this.dataService.getListByMap(page.pageToMap()));
		} catch (Exception e) {
			logger.error("查询 企业信息异常", e);
		}
	}

	public CommonResult deleteByKeyList(List<Integer> idList) {
		CommonResult result=new CommonResult();
		try {
			if(this.dataService.deleteByKeyList(idList))
			{
				result.setIsSuccess(true);
				result.setResultMsg("删除成功");
			}
			else
			{
				result.setResultMsg("删除失败");
			}
		} catch (Exception e) {
			logger.error("删除企业信息异常", e);
			result.setResultMsg("删除异常");
		}
		return result;
	}
}
